package com.base.util;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

public class BleManager {
    private static BleManager bleManager;
    private BluetoothAdapter bletAdapter;
    private List<BluetoothDevice> deviceList = new ArrayList<>();
    private final MutableLiveData<Boolean> _enabled = new MutableLiveData<>(false);
    private final LiveData<Boolean> enabled = _enabled;

    private BleManager() {
        bletAdapter = BluetoothAdapter.getDefaultAdapter();
        _enabled.postValue(bletAdapter.isEnabled());
    }

    public boolean supportBle() {
        return null != bletAdapter;
    }

    public static BleManager getInstance() {
        if (bleManager == null) {
            //同步锁
            synchronized (BleManager.class) {
                if (bleManager == null) {
                    bleManager = new BleManager();
                }
            }
        }
        return bleManager;
    }

    public void open() {
        Objects.requireNonNull(bletAdapter);
        boolean enable = true;
        if (!bletAdapter.isEnabled())
            enable = bletAdapter.enable();
        _enabled.postValue(enable);
        //bletAdapter.startLeScan(scanCallback);
    }

    public void open(Activity activity) {
        Objects.requireNonNull(bletAdapter);
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        activity.startActivityForResult(enableBtIntent, 2);
    }

    public void startDiscovery() {
        if (bletAdapter.isDiscovering())
            bletAdapter.cancelDiscovery();//q
        bletAdapter.startDiscovery();
        //bletAdapter.startLeScan(scanCallback);
    }
/*67:72:18:6D:84:14
5B:8D:7A:70:D6:5B
5B:8A:FA:D5:16:C6*/
    public void cancelDiscovery() {
        if (bletAdapter.isDiscovering())
            bletAdapter.cancelDiscovery();
    }

    public void close() {
        if (bletAdapter.isEnabled())
            bletAdapter.disable();
        _enabled.postValue(false);
    }

    public LiveData<Boolean> bleOpened() {
        return enabled;
    }

    /**
     * 已经配对的设备
     */
    /*private void findAvailableDevice() {
        //获取可配对蓝牙设备
        Set<BluetoothDevice> device = bluetoothAdapter.getBondedDevices();

        btList.clear();
        if (bluetoothAdapter != null && bluetoothAdapter.isDiscovering()) {
            btBoundAdapter.notifyDataSetChanged();
        }
        if (device.size() > 0) {
            //存在已经配对过的蓝牙设备
            for (BluetoothDevice btd : device) {
                btList.add(btd.getName() + '\n' + btd.getAddress());
                btBoundAdapter.notifyDataSetChanged();
            }
        } else {  //不存在已经配对过的蓝牙设备
            //btList.add("No can be matched to use bluetooth");
            btList.add("未发现已配对过的设备");
            btBoundAdapter.notifyDataSetChanged();
        }
    }*/
    BluetoothAdapter.LeScanCallback scanCallback = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
            Log.e("TAG", "run: scanning...");
            if (!deviceList.contains(device)) {
                deviceList.add(device);
                //deviceList.add(rssi);
                //mAdapter.notifyDataSetChanged();
            }
        }
    };
}
